export const underlineToHump = (str: string) => {
  if (!str) return str
  return str.replace(/_(\w)/g, (_, letter: string) => {
    return letter.toUpperCase()
  })
}

export const humpToUnderline = (str: string) => {
  return str.replace(/([A-Z])/g, '_$1').toLowerCase()
}

/**
 * 驼峰转横杠
 */
export const humpToDash = (str: string) => {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
  dom.style.setProperty(prop, val)
}

export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
  if (ary.findIndex) {
    return ary.findIndex(fn)
  }
  let index = -1
  ary.some((item: T, i: number, ary: Array<T>) => {
    const ret: T = fn(item, i, ary)
    if (ret) {
      index = i
      return ret
    }
  })
  return index
}

export const trim = (str: string) => {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}
